/**
******************************************************************************
* @file   		W5500.h
* @author  		WIZnet Software Team 
* @version 		V1.0
* @date    		2015-02-14
* @brief  		W5500寄存器地址定义/宏定义等
* @attention
******************************************************************************
**/
#ifndef  _W5500_H_
#define  _W5500_H_
#include "SPI_Driver.h"

/********************W5500在Includes.h中配置项*******************/
/*固件版本*/
#ifndef FW_VER
#define FW_VER					{ 1, 0 }
#endif // !FW_VER
//定义MAC地址,如果多块W5500网络适配板在同一现场工作，请使用不同的MAC地址(一般修改后3字节内容)
#ifndef DEFINE_MAC
#define DEFINE_MAC				{ 0x00, 0x08, 0xdc, 0x11, 0x11, 0x11 }
#endif // !DEFINE_MAC
/*定义W5500默认IP地址*/
#ifndef DEFINE_LOCAL_IP
#define DEFINE_LOCAL_IP			{ 192, 168, 1, 88 }
#endif // !DEFINE_LOCAL_IP
/*定义W5500默认子网掩码*/
#ifndef DEFINE_SUB_NET
#define DEFINE_SUB_NET			{ 255, 255, 255, 0 }
#endif // !DEFINE_SUB_NET
/*定义W5500默认网关*/
#ifndef DEFINE_GATEWAY
#define DEFINE_GATEWAY			{ 192, 168, 1, 1 }
#endif // !DEFINE_GATEWAY
/*定义W5500默认DNS*/
#ifndef DEFINE_DNS
#define DEFINE_DNS				{ 114, 114, 114, 114 }
#endif // !DEFINE_DNS
/*定义本地端口*/
#ifndef DEFINE_LOCAL_PORT
#define DEFINE_LOCAL_PORT		5000
#endif // !DEFINE_LOCAL_PORT
/*定义远端IP地址*/
#ifndef DEFINE_REMOTE_IP
#define DEFINE_REMOTE_IP		{ 192, 168, 1, 102 }
#endif // !DEFINE_REMOTE_IP
/*定义远端端口号*/
#ifndef DEFINE_REMOTE_PORT
#define DEFINE_REMOTE_PORT		5000
#endif // !DEFINE_REMOTE_PORT
/*IP配置方法选择，请自行选择: IP_FROM_DEFINE,IP_FROM_DHCP,IP_FROM_EEPROM*/
#ifndef IP_FROM_MODE
#define IP_FROM_MODE			IP_FROM_DEFINE
#endif // !IP_FROM_MODE
/**************************************************************/

#pragma region //=================W5500寄存器定义=====================
#pragma region //------------通用寄存器------------
#define MR                          (0x000000)	//模式寄存器
/* MODE register values */
#define MR_RST                       0x80 /**< reset */
#define MR_WOL                       0x20 /**< Wake on Lan */
#define MR_PB                        0x10 /**< ping block */
#define MR_PPPOE                     0x08 /**< enable pppoe */
#define MR_UDP_FARP                  0x02 /**< enbale FORCE ARP */
/**brief Gateway IP Register address网关IP地址寄存器*/
#define GAR0                        (0x000100)
#define GAR1                        (0x000200)
#define GAR2                        (0x000300)
#define GAR3                        (0x000400)
/**brief Subnet mask Register address子网掩码寄存器*/
#define SUBR0                       (0x000500)
#define SUBR1                       (0x000600)
#define SUBR2                       (0x000700)
#define SUBR3                       (0x000800)
/**brief Source MAC Register address源MAC地址寄存器*/
#define SHAR0                       (0x000900)
#define SHAR1                       (0x000A00)
#define SHAR2                       (0x000B00)
#define SHAR3                       (0x000C00)
#define SHAR4                       (0x000D00)
#define SHAR5                       (0x000E00)
/**@brief Source IP Register address源IP地址寄存器*/
#define SIPR0                       (0x000F00)
#define SIPR1                       (0x001000)
#define SIPR2                       (0x001100)
#define SIPR3                       (0x001200)
/**@brief set Interrupt low level timer register address低电平中断最小间隔定时器寄存器*/
#define INTLEVEL0                   (0x001300)
#define INTLEVEL1                   (0x001400)
/**@brief Interrupt Register中断寄存器*/
#define IR                          (0x001500)
/* IR register values */
#define IR_CONFLICT                  0x80 /**< check ip confict */
#define IR_UNREACH                   0x40 /**< get the destination unreachable message in UDP sending */
#define IR_PPPoE                     0x20 /**< get the PPPoE close message */
#define IR_MAGIC                     0x10 /**< get the magic packet interrupt */
/**@brief Interrupt mask register中断屏蔽寄存器*/
#define IMR                         (0x001600)
/**@brief Socket Interrupt Register*/ //Socket中断寄存器
#define SIR                         (0x001700)
/**@brief Socket Interrupt Mask Register*/ //Socket中断屏蔽寄存器
#define SIMR                        (0x001800)
/**@brief Timeout register address( 1 is 100us )*///重试时间寄存器,默认200ms
#define RTR0                        (0x001900)
#define RTR1                        (0x001A00)
/**@brief Retry count reigster*///重试次数寄存器,默认8次,故默认ART超时时间=(RTR*0.1ms)*(RCR+1)=1800ms,TCP超时时间=31.8s
#define WIZ_RCR                         (0x001B00)
/**@briefPPP LCP Request Timer register  in PPPoE mode*///PPP连接控制协议请求定时寄存器,默认40,*25ms=1s
#define PTIMER                      (0x001C00)
/**@brief PPP LCP Magic number register  in PPPoE mode*///PPP连接控制协议模数寄存器
#define PMAGIC                      (0x001D00)
/**@brief PPP Destination MAC Register address*///PPPoE模式下目标MAC寄存器
#define PDHAR0                      (0x001E00)
#define PDHAR1                      (0x001F00)
#define PDHAR2                      (0x002000)
#define PDHAR3                      (0x002100)
#define PDHAR4                      (0x002200)
#define PDHAR5                      (0x002300)
/**
 @brief PPP Session Identification Register//PPPoE模式下会话ID寄存器
 */
#define PSID0                       (0x002400)
#define PSID1                       (0x002500)
/**@brief PPP Maximum Segment Size(MSS) register*///PPPoE模式下最大接收单元,默认65535
#define PMR0                        (0x002600)
#define PMR1                        (0x002700)
/**@brief Unreachable IP register address in UDP mode*///无法抵达IP地址寄存器
#define UIPR0                       (0x002800)
#define UIPR1                       (0x002900)
#define UIPR2                       (0x002A00)
#define UIPR3                       (0x002B00)
/**@brief Unreachable Port register address in UDP mode*///无法抵达端口寄存器
#define UPORT0                      (0x002C00)
#define UPORT1                      (0x002D00)
/**@brief PHY Configuration Register*///W5500 PHY配置寄存器,默认=0xB8~0xBF
#define PHYCFGR                     (0x002E00)
/**@brief chip version register address*///W5500芯片版本寄存器,默认=0x04
#define VERSIONR                    (0x003900)
#pragma endregion

#pragma region //------------Socket端口寄存器-------------
/**@brief socket Mode register*///Socket-n模式寄存器
#define Sn_MR(ch)                       (0x000008 + (ch<<5))
/* Sn_MR values */
#define Sn_MR_CLOSE                  0x00     /**< unused socket */
#define Sn_MR_TCP                    0x01     /**< TCP */
#define Sn_MR_UDP                    0x02     /**< UDP */
#define Sn_MR_IPRAW                  0x03     /**< IP LAYER RAW SOCK */
#define Sn_MR_MACRAW                 0x04     /**< MAC LAYER RAW SOCK */
#define Sn_MR_PPPOE                  0x05     /**< PPPoE */
#define Sn_MR_UCASTB                 0x10     /**< Unicast Block in UDP Multicating*/
#define Sn_MR_ND                     0x20     /**< No Delayed Ack(TCP) flag */
#define Sn_MR_MC                     0x20     /**< Multicast IGMP (UDP) flag */
#define Sn_MR_BCASTB                 0x40     /**< Broadcast blcok in UDP Multicating */
#define Sn_MR_MULTI                  0x80     /**< support UDP Multicating */
/* Sn_MR values on MACRAW MODE */
#define Sn_MR_MIP6N                  0x10     /**< IPv6 packet Block */
#define Sn_MR_MMB                    0x20     /**< IPv4 Multicasting Block */
//#define Sn_MR_BCASTB                 0x40     /**< Broadcast blcok */
#define Sn_MR_MFEN                   0x80     /**< support MAC filter enable */

/**@brief channel Sn_CR register*///Socket-n命令/控制寄存器
#define Sn_CR(ch)                       (0x000108 + (ch<<5))
/* Sn_CR values */
#define Sn_CR_OPEN                   0x01     /**< initialize or open socket */
#define Sn_CR_LISTEN                 0x02     /**< wait connection request in tcp mode(Server mode) */
#define Sn_CR_CONNECT                0x04     /**< send connection request in tcp mode(Client mode) */
#define Sn_CR_DISCON                 0x08     /**< send closing reqeuset in tcp mode */
#define Sn_CR_CLOSE                  0x10     /**< close socket */
#define Sn_CR_SEND                   0x20     /**< update txbuf pointer, send data */
#define Sn_CR_SEND_MAC               0x21     /**< send data with MAC address, so without ARP process */
#define Sn_CR_SEND_KEEP              0x22     /**<  send keep alive message */
#define Sn_CR_RECV                   0x40     /**< update rxbuf pointer, recv data */
#ifdef __DEF_IINCHIP_PPP__
#define Sn_CR_PCON                0x23      
#define Sn_CR_PDISCON             0x24      
#define Sn_CR_PCR                 0x25      
#define Sn_CR_PCN                 0x26     
#define Sn_CR_PCJ                 0x27     
#endif

/**@brief channel interrupt register*///Socket-n中断寄存器
#define Sn_IR(ch)                       (0x000208 + (ch<<5))
/* Sn_IR values */
#define Sn_IR_SEND_OK                0x10     /**< complete sending */
#define Sn_IR_TIMEOUT                0x08     /**< assert timeout */
#define Sn_IR_RECV                   0x04     /**< receiving data */
#define Sn_IR_DISCON                 0x02     /**< closed socket */
#define Sn_IR_CON                    0x01     /**< established connection */
#ifdef __DEF_IINCHIP_PPP__
#define Sn_IR_PRECV               0x80     
#define Sn_IR_PFAIL               0x40     
#define Sn_IR_PNEXT               0x20     
#endif

/**@brief channel status register*///Socket-n状态寄存器
#define Sn_SR(ch)                       (0x000308 + (ch<<5))
/* Sn_SR values */
#define SOCK_CLOSED                  0x00     /**< closed */
#define SOCK_INIT                    0x13     /**< init state */
#define SOCK_LISTEN                  0x14     /**< listen state */
#define SOCK_SYNSENT                 0x15     /**< connection state */
#define SOCK_SYNRECV                 0x16     /**< connection state */
#define SOCK_ESTABLISHED             0x17     /**< success to connect */
#define SOCK_FIN_WAIT                0x18     /**< closing state */
#define SOCK_CLOSING                 0x1A     /**< closing state */
#define SOCK_TIME_WAIT               0x1B     /**< closing state */
#define SOCK_CLOSE_WAIT              0x1C     /**< closing state */
#define SOCK_LAST_ACK                0x1D     /**< closing state */
#define SOCK_UDP                     0x22     /**< udp socket */
#define SOCK_IPRAW                   0x32     /**< ip raw mode socket */
#define SOCK_MACRAW                  0x42     /**< mac raw mode socket */
#define SOCK_PPPOE                   0x5F     /**< pppoe socket */

/**@brief source port register*///Socket-n源端口寄存器
#define Sn_PORT0(ch)                    (0x000408 + (ch<<5))
#define Sn_PORT1(ch)                    (0x000508 + (ch<<5))
/**@brief Peer MAC register address*///Socket-n对方/目标MAC地址寄存器
#define Sn_DHAR0(ch)                    (0x000608 + (ch<<5))
#define Sn_DHAR1(ch)                    (0x000708 + (ch<<5))
#define Sn_DHAR2(ch)                    (0x000808 + (ch<<5))
#define Sn_DHAR3(ch)                    (0x000908 + (ch<<5))
#define Sn_DHAR4(ch)                    (0x000A08 + (ch<<5))
#define Sn_DHAR5(ch)                    (0x000B08 + (ch<<5))
/**@brief Peer IP register address*///Socket-n对方/目标IP寄存器
#define Sn_DIPR0(ch)                    (0x000C08 + (ch<<5))
#define Sn_DIPR1(ch)                    (0x000D08 + (ch<<5))
#define Sn_DIPR2(ch)                    (0x000E08 + (ch<<5))
#define Sn_DIPR3(ch)                    (0x000F08 + (ch<<5))
/**@brief Peer port register address*///Socket-n对方/目标端口寄存器
#define Sn_DPORT0(ch)                   (0x001008 + (ch<<5))
#define Sn_DPORT1(ch)                   (0x001108 + (ch<<5))
/**@brief Maximum Segment Size(Sn_MSSR0) register address*///Socket-n最大传输分段大小寄存器
#define Sn_MSSR0(ch)                    (0x001208 + (ch<<5))
#define Sn_MSSR1(ch)                    (0x001308 + (ch<<5))
//???
#define Sn_PROTO(ch)                    (0x001408 + (ch<<5))
/** @brief IP Type of Service(TOS) Register *///Socket-nIP服务类型寄存器
#define Sn_TOS(ch)                      (0x001508 + (ch<<5))
/**@brief IP Time to live(TTL) Register *///Socket-nIP生存时间寄存器
#define Sn_TTL(ch)                      (0x001608 + (ch<<5))
/**@brief Receive memory size reigster*///Socket-n接收缓冲区大小寄存器,[0x02]
#define Sn_RXMEM_SIZE(ch)               (0x001E08 + (ch<<5))
/**@brief Transmit memory size reigster*///Socket-n发送缓冲区大小寄存器,[0x02]
#define Sn_TXMEM_SIZE(ch)               (0x001F08 + (ch<<5))
/**@brief Transmit free memory size register*///Socket-n发送空置的缓冲区大小寄存器,[0x0800]
#define Sn_TX_FSR0(ch)                  (0x002008 + (ch<<5))
#define Sn_TX_FSR1(ch)                  (0x002108 + (ch<<5))
/**@brief Transmit memory read pointer register address*///Socket-n发送读指针寄存器
#define Sn_TX_RD0(ch)                   (0x002208 + (ch<<5))
#define Sn_TX_RD1(ch)                   (0x002308 + (ch<<5))
/**@brief Transmit memory write pointer register address*///Socket-n发送写指针寄存器
#define Sn_TX_WR0(ch)                   (0x002408 + (ch<<5))
#define Sn_TX_WR1(ch)                   (0x002508 + (ch<<5))
/**@brief Received data size register*///Socket-n收到数据大小寄存器
#define Sn_RX_RSR0(ch)                  (0x002608 + (ch<<5))
#define Sn_RX_RSR1(ch)                  (0x002708 + (ch<<5))
/**@brief Read point of Receive memory*///Socket-n接收读指针寄存器
#define Sn_RX_RD0(ch)                   (0x002808 + (ch<<5))
#define Sn_RX_RD1(ch)                   (0x002908 + (ch<<5))
/**@brief Write point of Receive memory*///Socket-n接收写指针寄存器
#define Sn_RX_WR0(ch)                   (0x002A08 + (ch<<5))
#define Sn_RX_WR1(ch)                   (0x002B08 + (ch<<5))
/**@brief socket interrupt mask register*///Socket-n中断屏蔽寄存器,[0xFF]
#define Sn_IMR(ch)                      (0x002C08 + (ch<<5))
/**@brief frag field value in IP header register*///Socket-n 分段寄存器
#define Sn_FRAG(ch)                     (0x002D08 + (ch<<5))
/**@brief Keep Timer register*///Socket-n在线时间寄存器
#define Sn_KPALVTR(ch)                  (0x002F08 + (ch<<5))
#pragma endregion

/* IP PROTOCOL */
#define IPPROTO_IP                   0        /**< Dummy for IP */
#define IPPROTO_ICMP                 1        /**< Control message protocol */
#define IPPROTO_IGMP                 2        /**< Internet group management protocol */
#define IPPROTO_GGP                  3        /**< Gateway^2 (deprecated) */
#define IPPROTO_TCP                  6        /**< TCP */
#define IPPROTO_PUP                  12       /**< PUP */
#define IPPROTO_UDP                  17       /**< UDP */
#define IPPROTO_IDP                  22       /**< XNS idp */
#define IPPROTO_ND                   77       /**< UNOFFICIAL net disk protocol */
#define IPPROTO_RAW                  255      /**< Raw IP packet */

#define	MAX_SOCK_NUM				8

#define IP_FROM_DEFINE	        0       			              /*使用初始定义的IP信息*/
#define IP_FROM_DHCP	        1       			              /*使用DHCP获取IP信息*/
#define IP_FROM_EEPROM	        2       			              /*使用EEPROM定义的IP信息*/
#pragma endregion

#pragma pack(1)
//W5500通信数据协议
typedef union _w5500spi
{
	uint8_t Byte[3];
	struct 
	{
		uint16_t RegAddr; //地址段:寄存器地址,发送时高位在先!
		struct
		{
			uint8_t OM	:	2;	//控制段:SPI工作/操作模式位
			uint8_t	RWB	:	1;	//控制段:读写位
			uint8_t	BSB	:	5;	//控制段:区域选择位
		};
	};
}W5500_Addr_Ctrl_t;
typedef union
{
	uint8_t Byte[4];
	struct 
	{
		uint16_t RegAddr; //地址段:寄存器地址,发送时高位在先!
		struct
		{
			uint8_t OM	:	2; //控制段:SPI工作/操作模式位
			uint8_t	RWB	:	1; //控制段:读写位
			uint8_t	BSB	:	5; //控制段:区域选择位
		};
		uint8_t	BData;	//数据段:字节数据
	};
}W5500_AddrCtrlByte_t;
typedef union
{
	uint8_t Byte[5];
	struct 
	{
		uint16_t RegAddr; //地址段:寄存器地址,发送时高位在先!
		struct
		{
			uint8_t OM	:	2; //控制段:SPI工作/操作模式位
			uint8_t	RWB	:	1; //控制段:读写位
			uint8_t	BSB	:	5; //控制段:区域选择位
		};
		uint16_t WData;	//数据段:16位数据
	};
}W5500_AddrCtrlWord_t;
#pragma pack()

#pragma pack(1)
/*此结构体定义了W5500可供配置的主要参数*/
typedef struct _CONFIG_MSG											
{
	uint8_t mac[6]; /*MAC地址*/
	uint8_t lip[4]; /*local IP本地IP地址*/
	uint8_t sub[4]; /*子网掩码*/
	uint8_t gw[4]; /*网关*/	
	uint8_t dns[4]; /*DNS服务器地址*/
	uint8_t rip[4]; /*remote IP远程IP地址*/
	uint8_t sw_ver[2]; /*软件版本号*/
}CONFIG_MSG;
#pragma pack()

#pragma pack(1)
/*此结构体定义了eeprom写入的几个变量，可按需修改*/
typedef struct //_EEPROM_MSG	                    
{
	uint8_t mac[6]; /*MAC地址*/
	uint8_t lip[4]; /*local IP本地IP地址*/
	uint8_t sub[4]; /*子网掩码*/
	uint8_t gw[4]; /*网关*/
	uint8_t remote_ip[4]; /* 远端IP地址*/
	uint16_t local_port; /*本地端口*/
	uint16_t remote_port; /* 远端端口号 */
	
	uint8_t analog_pos[4]; //采集模拟量占位:<0不采集,0,1...63表示采集的模拟量的存储位置,64,65...127表示采集的值变为开关档位后的存储位置-64
	uint8_t key_pos[10]; //采集开关量的储存位:<0不采集. 低4位表示在keys[]中字节偏移量,高4位表示在字节中的偏移位置
	uint8_t out_pos[15]; //输出模拟量:<0不使用. 0,1...127使用analogs[]的偏移位置的值输出,位于out_pos[0](R0)~[4](脉冲),[5](R1),[9](R2)
						 //输出开关量:<0不使用. 低3位表示在leds[]中字节偏移量,bit4=1表示使能步进电机,高3位表示在字节中的偏移位置.位于out_pos[5]~[14](O0~O9)
	uint8_t device_id; //设备识别码
	uint16_t analog_tap_pos[40]; //模拟量到开关档位量转换值:-1不转换. 0~16383为角度转换单位有效值域
	uint16_t freq_pwm_max[15]; //输出开关量打开时的pwm值,其中6..9包含4线步进电机驱动电压的pwm值
	uint16_t cal_pct[30]; //校准百分比0,...100; 3个传感器电阻分10档校准
	uint16_t cal_out[30]; //校准输出数据,对应百分比
}EEPROM_MSG_STR;
typedef struct
{
	uint8_t sync; //同步头0x55
	uint8_t frame_type; //帧类型
	uint8_t link_no; //连接识别码:0(最低层,没有下连信号),1(收到低层信号0之后),2...7;0~3bit=最大连接号,4~7bit=当前连接号
	uint8_t hw_ver;	//固件版本
	uint8_t eeprom_var[sizeof(EEPROM_MSG_STR) - 30 * 4]; //EEPROM_MSG_STR eeprom_var;
	uint8_t check_sum; //除同步头外其余所有字节和取低8位
}config_t;
#pragma pack()

#pragma region //===========公共变量==========
extern uint8_t local_ip[4];
extern uint16_t local_port; /*定义本地端口*/
extern uint8_t  remote_ip[4]; /* 远端IP地址*/
extern uint16_t remote_port; /* 远端端口号 */
extern uint8_t	ip_from;/*IP配置方法选择，请自行选择*/
extern uint8_t txsize[];
extern uint8_t rxsize[];
extern EEPROM_MSG_STR EEPROM_MSG; /*EEPROM存储信息结构体*/
#pragma endregion

#pragma region //===============方法API==============

/*
********************************************************************************
** 描述: 初始化W5500使用的SPI口,复位W5500,配置MAC地址,配置IP地址,初始化8个Socket的发送接收缓存大小.
** 返回: 0,1：成功，其它：失败
********************************************************************************/
int W5500_Init(SPI_HandleTypeDef *hspi/*[I]指向SPI句柄*/, osSemaphoreId done_sem/*[I]SPI通讯完成信号量句柄*/);
/*
********************************************************************************
**描述: 软复位W5500  reset iinchip
*******************************************************************************/
void iinchip_init(void);

#pragma endregion

/**
*@brief		写入一个8位数据到W5500
*@param		addrbsb: 写入数据的地址
*@param   data：写入的8位数据
*@return	无
*/
void IINCHIP_WRITE(uint32_t addrbsb, uint8_t data);
/**
*@brief		从W5500读出一个8位数据
*@param		addrbsb: 写入数据的地址
*@param   data：从写入的地址处读取到的8位数据
*@return	无
*/
uint8_t IINCHIP_READ(uint32_t addrbsb);
/*
********************************************************************************
** 描述: 向W5500的addrbsb写入16bit数据.addrbsb低8位为控制段,addrbsb的8位以上为地址段.
********************************************************************************/
void W5500_WriteWord(uint32_t addrbsb, uint16_t data);
/**
*@brief		从W5500读出len字节数据
*@param		addrbsb: 读取数据的地址
*@param 	buf：存放读取数据
*@param		len：字符串长度
*@return	len：返回字符串长度
*/
uint16_t wiz_read_buf(uint32_t addrbsb, uint8_t* pRx_buf, uint16_t len);
/**
*@brief		向W5500写入len字节数据
*@param		addrbsb: 写入数据的地址
*@param   buf：写入字符串
*@param   len：字符串长度
*@return	len：返回字符串长度
*/
uint16_t wiz_write_buf(uint32_t addrbsb, uint8_t* pCh, uint16_t len);

/*W5500基本配置相关函数*/
void reset_w5500(void); /*硬复位W5500*/
void set_w5500_mac(void); /*配置W5500的MAC地址*/
void set_w5500_ip(void); /*配置W5500的IP地址*/

void socket_buf_init(uint8_t * tx_size, uint8_t * rx_size); // setting tx/rx buf size
uint8_t getISR(uint8_t s);
void putISR(uint8_t s, uint8_t val);
uint16_t getIINCHIP_RxMAX(uint8_t s);
uint16_t getIINCHIP_TxMAX(uint8_t s);
/*
********************************************************************************
 *描述: 设置W5500的MR寄存器
 ******************************************************************************/
void setMR(uint8_t val);
void setRTR(uint16_t timeout); // set retry duration for data transmission, connection, closing ...
void setRCR(uint8_t retry); // set retry count (above the value, assert timeout interrupt)
void clearIR(uint8_t mask); // clear interrupt
uint8_t getIR( void );
void setSn_MSS(uint8_t s, uint16_t Sn_MSSR); // set maximum segment size
uint8_t getSn_IR(uint8_t s); // get socket interrupt status
uint8_t getSn_SR(uint8_t s); // get socket status
uint16_t getSn_TX_FSR(uint8_t s); // get socket TX free buf size
uint16_t getSn_RX_RSR(uint8_t s); // get socket RX recv buf size
uint8_t getSn_SR(uint8_t s);
void setSn_TTL(uint8_t s, uint8_t ttl);
void send_data_processing(uint8_t s, uint8_t *wizdata, uint16_t len);
void recv_data_processing(uint8_t s, uint8_t *wizdata, uint16_t len);

/*
********************************************************************************
 *描述: 设置网关IP地址
 ******************************************************************************/
void setGAR(uint8_t * addr); // set gateway address
void setSUBR(uint8_t * addr); // set subnet mask address
void setSHAR(uint8_t * addr); // set local MAC address
void setSIPR(uint8_t * addr); // set local IP address
/*
********************************************************************************
 *描述: 获得网关IP地址
 ******************************************************************************/
void getGAR(uint8_t * addr);
void getSUBR(uint8_t * addr);
void getSHAR(uint8_t * addr);
void getSIPR(uint8_t * addr);

void setSn_IR(uint8_t s, uint8_t val);
void Reset_W5500(void);

/**
 @brief WIZCHIP_OFFSET_INC on IINCHIP_READ/WRITE
 * case1.
 *  IINCHIP_WRITE(RTR0,val);
 *  IINCHIP_WRITE(RTR1,val);
 * case1. 
 *  IINCHIP_WRITE(RTR0,val);
 *  IINCHIP_WRITE(WIZCHIP_OFFSET_INC(RTR0,1)); 
 */
//#define WIZCHIP_OFFSET_INC(ADDR, N)    (ADDR + (N<<8)) //< Increase offset address

#endif
